package com.briup.result;

import com.briup.enums.CodeAndMessageEnum;
import lombok.Data;
@Data
public class Result<T> {

    /**
     * 响应状态码
     */
    private Integer code;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    public Result(CodeAndMessageEnum codeAndMessageEnum){
        this.code = codeAndMessageEnum.getCode();
        this.message = codeAndMessageEnum.getMessage();
    }

    public Result(CodeAndMessageEnum codeAndMessageEnum,T data){
        this.code = codeAndMessageEnum.getCode();
        this.message = codeAndMessageEnum.getMessage();
        this.data = data;
    }


    /**
     * 成功
     *
     * @return {@link Result}<{@link ?}>
     */
    public static Result<?> success(){
        return new Result(CodeAndMessageEnum.SUCCESS);
    }

    /**
     * 错误
     *
     * @return {@link Result}<{@link ?}>
     */
    public static Result<?> error(){
        return new Result(CodeAndMessageEnum.ERROR);
    }

    /**
     * 成功（带数据返回）
     *
     * @param data 数据
     * @return {@link Result}<{@link ?}>
     */
    public static <T> Result<?> success(T data){
        return new Result(CodeAndMessageEnum.SUCCESS,data);
    }

    /**
     * 错误（枚举类）
     *
     * @param codeAndMessageEnum 代码和消息枚举
     * @return {@link Result}<{@link ?}>
     */
    public static Result<?> error(CodeAndMessageEnum codeAndMessageEnum){
        return new Result(codeAndMessageEnum);
    }
}
